


//
//  RPAppManager.m
//  Rippton2.0
//
//  Created by 胡旭辉 on 2019/7/12.
//  Copyright © 2019 胡旭辉. All rights reserved.
//

#import "RPAppManager.h"
#import "UserInfoModel.h"
#import <sys/utsname.h>
#import <Photos/PHPhotoLibrary.h>
@implementation RPAppManager
SingletonM(RPAppManager)

static NSDateFormatter *fmt_;
static NSCalendar *calendar_;
/**
 *  在第一次使用类时调用1次
 */
+ (void)initialize
{
    fmt_ = [[NSDateFormatter alloc] init];
    calendar_ = [NSCalendar calendar];
}

+ (BOOL)isLogin{
    return [[RPAppManager sharedRPAppManager] isLogin];
}

- (BOOL)isLogin{
    if ([UserInfoModel userInfo] != nil) {
        return YES;
    }
    return NO;
}

+ (BOOL)isLogin:(UIViewController *)vc{
    return YES;
    
}
+ (NSNumber *)getCustomerId{
    return [RPAppManager sharedRPAppManager].getCustomerId;
}
- (NSNumber *)getCustomerId{
    UserInfoModel *user = [UserInfoModel userInfo];
    return user.id == nil?@0:user.id;
}
+ (NSString *)getToken{
    return [RPAppManager sharedRPAppManager].getToken;
}
- (NSString *)getToken{
    UserInfoModel *user = [UserInfoModel userInfo];
    return user.userToken.length == 0?@"":user.userToken;
}

+ (UIColor *)themColor{
    return [RPAppManager sharedRPAppManager].themColor;
}
- (UIColor *)themColor{
    return RGBA(86, 128, 250, 1);
}

+ (CGSize)getTextSizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize{
    return [[RPAppManager sharedRPAppManager] getTextSizeWithText:text font:font maxSize:maxSize];
}

- (CGSize)getTextSizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    CGSize size = CGSizeZero;
    if(text == nil){
        text = @"";
    }
    if(font == nil){
        font = [UIFont systemFontOfSize:13];
    }
    NSDictionary *attribute = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
    size = [text boundingRectWithSize:maxSize options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return size;
}


+  (UIViewController *)currentViewController{
    return [[RPAppManager sharedRPAppManager] currentViewController];
}

- (UIViewController *)currentViewController{
    
    UIViewController * currVC = nil;
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    UIViewController * Rootvc = window.rootViewController;
    
    do {
        if ([Rootvc isKindOfClass:[UINavigationController class]]) {
            UINavigationController * nav = (UINavigationController *)Rootvc;
            UIViewController * v = [nav.viewControllers lastObject];
            currVC = v;
            Rootvc = v.presentedViewController;
            continue;
        }else if([Rootvc isKindOfClass:[UITabBarController class]]){
            UITabBarController * tabVC = (UITabBarController *)Rootvc;
            currVC = tabVC;
            Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
            continue;
        }
    } while (Rootvc!=nil);
    
    return currVC;
}


+ (NSDictionary *)dictionaryWithUrlString:(NSString *)urlStr{
    return [[RPAppManager sharedRPAppManager]dictionaryWithUrlString:urlStr];
}

- (NSDictionary *)dictionaryWithUrlString:(NSString *)urlStr{
    if (urlStr && urlStr.length && [urlStr rangeOfString:@"?"].length == 1) {
        NSArray *array = [urlStr componentsSeparatedByString:@"?"];
        if (array && array.count == 2) {
            NSString *paramsStr = array[1];
            if (paramsStr.length) {
                NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
                NSArray *paramArray = [paramsStr componentsSeparatedByString:@"&"];
                for (NSString *param in paramArray) {
                    if (param && param.length) {
                        NSArray *parArr = [param componentsSeparatedByString:@"="];
                        if (parArr.count == 2) {
                            [paramsDict setObject:parArr[1] forKey:parArr[0]];
                        }
                    }
                }
                return paramsDict;
            }else{
                return nil;
            }
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}
+(NSString *)getCurrentDeviceModel{
    return [[RPAppManager sharedRPAppManager]getCurrentDeviceModel];
}


-(NSString *)getCurrentDeviceModel{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    if ([deviceModel isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceModel isEqualToString:@"iPhone3,2"])    return @"iPhone 4";
    if ([deviceModel isEqualToString:@"iPhone3,3"])    return @"iPhone 4";
    if ([deviceModel isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([deviceModel isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    if ([deviceModel isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([deviceModel isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([deviceModel isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([deviceModel isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([deviceModel isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([deviceModel isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([deviceModel isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    // 日行两款手机型号均为日本独占，可能使用索尼FeliCa支付方案而不是苹果支付
    if ([deviceModel isEqualToString:@"iPhone9,1"])    return @"iPhone 7";
    if ([deviceModel isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus";
    if ([deviceModel isEqualToString:@"iPhone9,3"])    return @"iPhone 7";
    if ([deviceModel isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus";
    if ([deviceModel isEqualToString:@"iPhone10,1"])   return @"iPhone_8";
    if ([deviceModel isEqualToString:@"iPhone10,4"])   return @"iPhone_8";
    if ([deviceModel isEqualToString:@"iPhone10,2"])   return @"iPhone_8_Plus";
    if ([deviceModel isEqualToString:@"iPhone10,5"])   return @"iPhone_8_Plus";
    if ([deviceModel isEqualToString:@"iPhone10,3"])   return @"iPhone X";
    if ([deviceModel isEqualToString:@"iPhone10,6"])   return @"iPhone X";
    if ([deviceModel isEqualToString:@"iPhone11,8"])   return @"iPhone XR";
    if ([deviceModel isEqualToString:@"iPhone11,2"])   return @"iPhone XS";
    if ([deviceModel isEqualToString:@"iPhone11,6"])   return @"iPhone XS Max";
    if ([deviceModel isEqualToString:@"iPhone11,4"])   return @"iPhone XS Max";
    if ([deviceModel isEqualToString:@"iPhone12,1"])   return @"iPhone 11";
    if ([deviceModel isEqualToString:@"iPhone12,3"])   return @"iPhone 11 Pro";
    if ([deviceModel isEqualToString:@"iPhone12,5"])   return @"iPhone 11 Pro Max";
    if ([deviceModel isEqualToString:@"iPhone12,8"])   return @"iPhone SE2";
    if ([deviceModel isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([deviceModel isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([deviceModel isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([deviceModel isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([deviceModel isEqualToString:@"iPod5,1"])      return @"iPod Touch (5 Gen)";
    if ([deviceModel isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceModel isEqualToString:@"iPad1,2"])      return @"iPad 3G";
    if ([deviceModel isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad2,2"])      return @"iPad 2";
    if ([deviceModel isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([deviceModel isEqualToString:@"iPad2,4"])      return @"iPad 2";
    if ([deviceModel isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([deviceModel isEqualToString:@"iPad2,6"])      return @"iPad Mini";
    if ([deviceModel isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPad3,3"])      return @"iPad 3";
    if ([deviceModel isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad3,5"])      return @"iPad 4";
    if ([deviceModel isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([deviceModel isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([deviceModel isEqualToString:@"iPad4,4"])      return @"iPad Mini 2 (WiFi)";
    
    if ([deviceModel isEqualToString:@"iPad4,5"])      return @"iPad Mini 2 (Cellular)";
    if ([deviceModel isEqualToString:@"iPad4,6"])      return @"iPad Mini 2";
    if ([deviceModel isEqualToString:@"iPad4,7"])      return @"iPad Mini 3";
    if ([deviceModel isEqualToString:@"iPad4,8"])      return @"iPad Mini 3";
    if ([deviceModel isEqualToString:@"iPad4,9"])      return @"iPad Mini 3";
    if ([deviceModel isEqualToString:@"iPad5,1"])      return @"iPad Mini 4 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad5,2"])      return @"iPad Mini 4 (LTE)";
    if ([deviceModel isEqualToString:@"iPad5,3"])      return @"iPad Air 2";
    if ([deviceModel isEqualToString:@"iPad5,4"])      return @"iPad Air 2";
    if ([deviceModel isEqualToString:@"iPad6,3"])      return @"iPad Pro 9.7";
    if ([deviceModel isEqualToString:@"iPad6,4"])      return @"iPad Pro 9.7";
    if ([deviceModel isEqualToString:@"iPad6,7"])      return @"iPad Pro 12.9";
    if ([deviceModel isEqualToString:@"iPad6,8"])      return @"iPad Pro 12.9";

    if ([deviceModel isEqualToString:@"AppleTV2,1"])      return @"Apple TV 2";
    if ([deviceModel isEqualToString:@"AppleTV3,1"])      return @"Apple TV 3";
    if ([deviceModel isEqualToString:@"AppleTV3,2"])      return @"Apple TV 3";
    if ([deviceModel isEqualToString:@"AppleTV5,3"])      return @"Apple TV 4";

    if ([deviceModel isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceModel isEqualToString:@"x86_64"])       return @"Simulator";

    return deviceModel;
    
}

+ (void)updateNetworkStatus:(NSNumber *)status{
    [[RPAppManager sharedRPAppManager]updateNetworkStatus:status];
}
- (void)updateNetworkStatus:(NSNumber *)status{
    // 保存网络状态(可全局定义变量)
    [[NSUserDefaults standardUserDefaults]setObject:status forKey:@"NetworkStatus"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

+ (BOOL)isShowNetworkView{
    return [[RPAppManager sharedRPAppManager]isShowNetworkView];
}
- (BOOL)isShowNetworkView{
    return !([[[NSUserDefaults standardUserDefaults]objectForKey:@"NetworkStatus"] boolValue]);
}
+ (void)clearCahceData{
    [[RPAppManager sharedRPAppManager]clearCahceData];
}
- (void)clearCahceData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
        for (NSString *p in files) {
            NSString *path = [cachPath stringByAppendingPathComponent:p];
            NSError *error;
            if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                [[NSFileManager defaultManager]removeItemAtPath:path error:&error];
            }
        }
        [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];
    });
}
-(void)clearCacheSuccess
{
    [[SDImageCache sharedImageCache]calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{
            [[SDImageCache sharedImageCache]clearDiskOnCompletion:nil];
            [WHToast showImage:[UIImage imageNamed:@"toast_tag_success"] message:@"清理完成" duration:2 finishHandler:nil];
        });
   
   }];
}
+ (NSString *)md5StrWithApi:(NSString *)api{
   return [[RPAppManager sharedRPAppManager]md5StrWithApi:api];
}
- (NSString *)md5StrWithApi:(NSString *)api{
    
    
    NSString *str = [[api componentsSeparatedByString:@"/"] lastObject];
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyyMMddHH";
    NSString *dateStr = [formatter stringFromDate:date];
    return [[NSString stringWithFormat:@"%@xyk2020%@",str,dateStr] MD5Str];
}

+(void)saveUserPlanIdsWithPlans:(NSArray *)plans{
    [[RPAppManager sharedRPAppManager]saveUserPlanIdsWithPlans:plans];
    
}

-(void)saveUserPlanIdsWithPlans:(NSArray *)plans{
    NSString *key = [NSString stringWithFormat:@"%@/getUserPlanList",[RPAppManager getCustomerId]];
    [[NSUserDefaults standardUserDefaults]setObject:plans forKey:key];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

+ (NSArray *)getOwnerPlanIds{
   return [[RPAppManager sharedRPAppManager]getOwnerPlanIds];
}

- (NSArray *)getOwnerPlanIds{
    NSString *key = [NSString stringWithFormat:@"%@/getUserPlanList",[RPAppManager getCustomerId]];
    NSArray *arr = [[NSUserDefaults standardUserDefaults]objectForKey:key];
    if (arr == nil) {
        return @[];
    }
    return arr;
}

+(void)deleteUserAllPlans{
    return [[RPAppManager sharedRPAppManager]deleteUserAllPlans];
}

-(void)deleteUserAllPlans{
    NSString *key = [NSString stringWithFormat:@"%@/getUserPlanList",[RPAppManager getCustomerId]];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:key];
}
+(void)deleteUserPlanById:(NSString *)id{
    [[RPAppManager sharedRPAppManager]deleteUserPlanById:id];
}
-(void)deleteUserPlanById:(NSString *)id{
    NSString *key = [NSString stringWithFormat:@"%@/getUserPlanList",[RPAppManager getCustomerId]];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:key]];
    __block NSInteger index = -1;
    [arr enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if([obj.stringValue isEqualToString:id]){
            index = idx;
            *stop = YES;
        }
    }];
    if(index != -1){
        [arr removeObjectAtIndex:index];
    }
    [[NSUserDefaults standardUserDefaults]setObject:arr forKey:key];
    [[NSUserDefaults standardUserDefaults]synchronize];

    
}


+ (NSString *)created_atWithCreateDate:(long)createDate andCurrentDate:(long)currentDate{
    return [[RPAppManager sharedRPAppManager] created_atWithCreateDate:createDate andCurrentDate:currentDate];
}
- (NSString *)created_atWithCreateDate:(long)createDate andCurrentDate:(long)currentDate{
    NSDate *date = [NSDate dataWithTimeSp:createDate];
    NSString *timeStr = [date getTimeWithFormatter:@"yyyy-MM-dd HH:mm:ss"];

    NSDateFormatter *fmt_ = [[NSDateFormatter alloc] init];
    NSCalendar *calendar_ = [NSCalendar calendar];
    
    if (currentDate == 0) {
        currentDate = [NSDate date].timeIntervalSince1970 * 1000;
    }
    
    fmt_.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *createdAtDate = [fmt_ dateFromString:timeStr];
    
    if (createdAtDate.isThisYear) { // 今年
        if (createdAtDate.isToday) { // 今天
            NSDate *nowDate = [NSDate dataWithTimeSp:currentDate];
            NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
            NSDateComponents *cmps = [calendar_ components:unit fromDate:createdAtDate toDate:nowDate options:0];
            if (cmps.hour >= 1) { // 时间间隔 >= 1小时
                return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
            }else{
                if (cmps.minute <= 0) {
                    return @"刚刚";
                }
                return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
            }
        }else {
            fmt_.dateFormat = @"M-d";
            NSString *monthDay = [fmt_ stringFromDate:createdAtDate];
            return [NSString stringWithFormat:@"%@",monthDay];
        }
    } else {
        fmt_.dateFormat = @"yyyy-M-d";
        NSString *year = [fmt_ stringFromDate:createdAtDate];
        return [NSString stringWithFormat:@"%@",year];
    }
}


+(UIImage*)imageByCroppingWithImage:(UIImage*)myImage andRect:(CGRect)rect{
    return [[RPAppManager sharedRPAppManager]imageByCroppingWithImage:myImage andRect:rect];
}

-(UIImage*)imageByCroppingWithImage:(UIImage*)myImage andRect:(CGRect)rect;
{
    // oc 对应的是点 c语言的方法对应的像素，所以要用的scale
    CGImageRef newImgRef = CGImageCreateWithImageInRect(myImage.CGImage,rect);
    UIImage *newImg = [UIImage imageWithCGImage:newImgRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
    
    CGImageRelease(newImgRef);
    
    return newImg;
}


+ (NSComparisonResult)compareOnlineVersion:(NSString*)versionOne toVersion:(NSString*)versionTwo{
    return [[RPAppManager sharedRPAppManager] compareOnlineVersion:versionOne toVersion:versionTwo];
}
- (NSComparisonResult)compareOnlineVersion:(NSString*)versionOne toVersion:(NSString*)versionTwo {
    
    NSArray* versionOneArr = [versionOne componentsSeparatedByString:@"."];
    NSArray* versionTwoArr = [versionTwo componentsSeparatedByString:@"."];
    NSInteger pos = 0;
    
    while ([versionOneArr count] > pos || [versionTwoArr count] > pos) {
        
        NSInteger v1 = [versionOneArr count] > pos ? [[versionOneArr objectAtIndex:pos] integerValue] : 0;
        
        NSInteger v2 = [versionTwoArr count] > pos ? [[versionTwoArr objectAtIndex:pos] integerValue] : 0;
        
        if (v1 < v2) {
            
            //版本2大
            return NSOrderedAscending;
            
        }
        else if (v1 > v2) {
            
            //版本1大
            return NSOrderedDescending;
        }
        pos++;
    }
   //版本相同
    return NSOrderedSame;
    
}



+ (BOOL)checkVideoCameraAuthorization{
    return [[RPAppManager sharedRPAppManager]checkVideoCameraAuthorization];
}

- (BOOL)checkVideoCameraAuthorization{
    __block BOOL isAvalible = NO;
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch (status) {
        case AVAuthorizationStatusAuthorized: //授权
            isAvalible = YES;
            break;
        case AVAuthorizationStatusDenied:
            break;
        case AVAuthorizationStatusNotDetermined:
            isAvalible = YES;
            break;
        case AVAuthorizationStatusRestricted:  //受限制，家长控制器
            isAvalible = NO;
            break;
    }
    return isAvalible;
}
+ (BOOL)checkVideoPhotoAuthorization{
    return [[RPAppManager sharedRPAppManager] checkVideoPhotoAuthorization];
}

- (BOOL)checkVideoPhotoAuthorization{
    __block BOOL isAvalible = NO;
    //iOS8.0之后
    PHAuthorizationStatus photoStatus = [PHPhotoLibrary authorizationStatus];
    if (@available(iOS 14, *)) {
        PHAccessLevel level = PHAccessLevelReadWrite;
        photoStatus = [PHPhotoLibrary authorizationStatusForAccessLevel:level];
    } else {
       
    }
    switch (photoStatus) {
        case PHAuthorizationStatusAuthorized:
            isAvalible = YES;
            break;
        case PHAuthorizationStatusDenied:
            isAvalible = NO;
            break;
        case PHAuthorizationStatusNotDetermined:
            isAvalible = YES;
            break;
        case PHAuthorizationStatusRestricted:
            isAvalible = NO;
            break;
        default:
            break;
    }
    return isAvalible;
}


+ (BOOL)checkVideoMicrophoneAudioAuthorization{
    return [[RPAppManager sharedRPAppManager]checkVideoMicrophoneAudioAuthorization];
}

- (BOOL)checkVideoMicrophoneAudioAuthorization{
    __block BOOL isAvalible = NO;
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    switch (status) {
        case AVAuthorizationStatusAuthorized: //授权
            isAvalible = YES;
            break;
        case AVAuthorizationStatusDenied:   //拒绝，弹框
           isAvalible = NO;
            break;
        case AVAuthorizationStatusNotDetermined:   //没有决定，第一次启动
            isAvalible = YES;
            break;
        case AVAuthorizationStatusRestricted:  //受限制，家长控制器
            isAvalible = NO;
            break;
    }
    return isAvalible;
}

+ (NSString *)notRounding:(CGFloat)price afterPoint:(int)position{
    return [[RPAppManager sharedRPAppManager]notRounding:price afterPoint:position];
}

- (NSString *)notRounding:(CGFloat)price afterPoint:(int)position{
 
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
 
    NSDecimalNumber *ouncesDecimal;
 
    NSDecimalNumber *roundedOunces;
 
    ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
 
    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];

    return [NSString stringWithFormat:@"%.1f",roundedOunces.floatValue];
}


+ (void)removeLaunchScreenCacheIfNeeded{
    return [[RPAppManager sharedRPAppManager]removeLaunchScreenCacheIfNeeded];
}

- (void)removeLaunchScreenCacheIfNeeded {
   NSString *filePath = [NSString stringWithFormat:@"%@/Library/SplashBoard", NSHomeDirectory()];
   
   if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    NSError *error = nil;
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];

    if (error) {
         NSLog(@"清除LaunchScreen缓存失败");
       } else {
         NSLog(@"清除LaunchScreen缓存成功");
       }
    }
}


+ (UIViewController *)jsd_findVisibleViewController{
    return [[RPAppManager sharedRPAppManager]jsd_findVisibleViewController];
}

- (UIViewController *)jsd_findVisibleViewController {
    
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    UIViewController* currentViewController = window.rootViewController;

    BOOL runLoopFind = YES;
    while (runLoopFind) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        } else {
            if ([currentViewController isKindOfClass:[UINavigationController class]]) {
                currentViewController = ((UINavigationController *)currentViewController).visibleViewController;
            } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
                currentViewController = ((UITabBarController* )currentViewController).selectedViewController;
            } else {
                break;
            }
        }
    }
    return currentViewController;
}


+(void)openUrl:(NSString *)url andConfigInfo:(NSDictionary *)configInfo{
    return [[RPAppManager sharedRPAppManager]openUrl:url andConfigInfo:configInfo];
}

-(void)openUrl:(NSString *)url andConfigInfo:(NSDictionary *)configInfo{
    id cls = [[NSClassFromString(url)alloc]init];
    if ([cls isKindOfClass:[RPBaseViewController class]]) {
        ((RPBaseViewController *)cls).param = configInfo;
        [[RPAppManager jsd_findVisibleViewController].navigationController pushViewController:(RPBaseViewController *)cls animated:YES];
    }
}


@end
